import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;


public class Registration {
	private ArrayList<Vehicle> vehicles ;
	private int numberOfVehicles;
	int levy = 10;

	
	public Registration()
	{
		vehicles = new ArrayList<>();
		numberOfVehicles = 0;

	}
	public void addNewCar(String rego, String make, String model, String ownername,
						  String address, Boolean status, int numcyl, String colour) {
		Vehicle car = new PrivateCar(rego, make, model, ownername, address, status, numcyl, colour);
		this.vehicles.add(car);
		numberOfVehicles++;
	}
	public void addNewBike(String rego, String make, String model, String ownername,
			  String address, Boolean status, int engineCapacity) {
		Motorbikes mbyc = new Motorbikes(rego, make, model, ownername, address, status, engineCapacity);
		this.vehicles.add(mbyc);
		numberOfVehicles++;
	}
	public void addNewCommercial(String rego, String make, String model, String ownername,
			  String address, Boolean status, int load, int passengers) {
		CommericalVehicle cevh = new CommericalVehicle(rego, make, model, ownername, address, status, load, passengers);
		this.vehicles.add(cevh);
		numberOfVehicles++;
	}
	public Vehicle getVehicleAt(int index) {
		return this.vehicles.get(index);
	}
	public void registerVehicle(String rego) {
		
		for(int i = 0; i < numberOfVehicles; i++)
		{
			if(vehicles.get(i).GetregNum() == rego)
			{
				if(vehicles.get(i).regStatus == false)
					vehicles.get(i).SetregStatus(true);
			}
			return;
		}
	}
	public void canelRego(String rego) {
		for(int i = 0; i < numberOfVehicles; i++)
		{
			if(vehicles.get(i).GetregNum() == rego)
			{
				if(vehicles.get(i).regStatus == true)
					vehicles.get(i).SetregStatus(false);
			}
			return;
		}
	}
	public ArrayList<Vehicle> listByType(String type)  {
		ArrayList<Vehicle> typeVehicle = new ArrayList<>();
		if(type == "PCAR")
		{
			for(int i = 0; i < numberOfVehicles; i++)
			{
				if(vehicles.get(i) instanceof PrivateCar)
					typeVehicle.add(vehicles.get(i));
			}
		}
		if(type == "CVEH")
		{
			for(int i = 0; i < numberOfVehicles; i++)
			{
				if(vehicles.get(i) instanceof CommericalVehicle)
					typeVehicle.add(vehicles.get(i));
			}
		}
		if(type == "MBYC")
		{
			for(int i = 0; i < numberOfVehicles; i++)
			{
				if(vehicles.get(i) instanceof Motorbikes)
					typeVehicle.add(vehicles.get(i));
			}
		}
		return typeVehicle;
		
	}
	public ArrayList<Double> listRegoFees() {
		ArrayList<Double> RegoFees = new ArrayList<>();
		for(int i = 0; i < numberOfVehicles; i++)
		{
			Double fee = vehicles.get(i).calculateRego();
			RegoFees.add(fee);
		}
		return RegoFees;
	}
	public ArrayList<String> listRegistrations() {
		ArrayList<String> Registrations = new ArrayList<>();
		for(int i = 0; i < numberOfVehicles; i++)
		{
			String registration = vehicles.get(i).GetregNum();
			Registrations.add(registration);
		}
		return Registrations;
	}
	public void changeVictimsLevy(int newLevy) {
		levy = newLevy;
	}
	public double regoFeeByRego(String rego) {
		for(int i = 0; i < numberOfVehicles; i++)
		{
			if(vehicles.get(i).GetregNum() == rego)
			{
				double fee = vehicles.get(i).calculateRego();
				return fee;
			}
		}
		return 0;
	}
	public void importData() {
		
		Readfile readfile = new Readfile();
		String st = readfile.openfile();
		String importlist[] = st.split("\\n");
		for (int i = 0;i < importlist.length;i++)
		{
			String item[] = importlist[i].split("\\|");
				//check status
				boolean status = false;
				if (item[6].toString().equals("current"))
				{
					status = true;
				}
				switch (item[0])
				{
				case "PCAR":
					int a = Integer.parseInt(item[7]);
					addNewCar(item[1], item[2], item[3], item[4], item[5], status, a, item[8]);
					break;
				case "CVEH":
					addNewCommercial(item[1].toString(), item[2].toString(), item[3].toString(), item[4].toString(), item[5].toString(), status, Integer.parseInt(item[7].toString()), Integer.parseInt(item[8].toString()));
					break;
				case "MBYC":
					addNewBike(item[1].toString(), item[2].toString(), item[3].toString(), item[4].toString(), item[5].toString(), status, Integer.parseInt(item[7].toString()));
					break;
				default:
					break;
				}
			
		}
		setNumberOfVehicles(vehicles.size());
			
	}
	public void clearData() {
		
	}
	public String toString() {
		for (int i =0;i<vehicles.size();i++)
		{
			System.out.println(vehicles.get(i).toString());
		}
		return "";
	}
	public void exit() throws IOException {
		String pathfile = "mvreg.txt";
		
		FileOutputStream fileOutputStream = new FileOutputStream(pathfile);
		Writer writer = new OutputStreamWriter(fileOutputStream,"UTF8");
		for(int i = 0; i < numberOfVehicles; i++)
		{
			String regStatus;
			if(vehicles.get(i).GetregStatus() == true)
				regStatus = "current";
			else;
				regStatus = "expire";
				String current = vehicles.get(i).GetregNum() + "|" + vehicles.get(i).GetmakeDescription() + "|"
						+ vehicles.get(i).GetmodelDescription() + "|"  + vehicles.get(i).GetregOwnerName() + "|" 
						+ vehicles.get(i).GetregOwnerAddress() + "|" + regStatus + "|";
			if(vehicles.get(i) instanceof PrivateCar)
			{
				PrivateCar pcar = (PrivateCar) vehicles.get(i);
				String temp = "PCAR|" + current + Integer.toString(pcar.GetnumCyl()) + "|" + pcar.Getcolor() + "\r\n";
				writer.write(temp);
			}
			if(vehicles.get(i) instanceof CommericalVehicle)
			{
				CommericalVehicle cveh = (CommericalVehicle) vehicles.get(i);
				String temp = "CVEH|" + current + Integer.toString(cveh.GetloadCapacity()) + "|" + Integer.toString(cveh.GetnumPassenger()) + "\r\n";
				writer.write(temp);
			}
			if(vehicles.get(i) instanceof Motorbikes)
			{
				Motorbikes mbyc = (Motorbikes)vehicles.get(i);
				String temp = "MBYC|" + current + Integer.toString(mbyc.GetengineCapacity()) + "\r\n";
				writer.write(temp);
			}
		}
		writer.close();
	}
	public int getNumberOfVehicles() {
		return numberOfVehicles;
	}
	public void setNumberOfVehicles(int numberOfVehicles) {
		this.numberOfVehicles = numberOfVehicles;
	}

}
